package cn.tedu.v1_1;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * 聊天室 Version 1.1
 * 功能:1.客户端--指定IP,端口号
 *     2.客户端给服务器发送文本
 *     3.客户端完成标准IO
 *[新的]4.捕获异常,打印异常信息--全给他们逮住,给用户提示
 *     5.关闭socket
 *
 */
public class Client {
    private Socket socket;
    public Client(){
        try {
            socket=new Socket("172.22.160.1",886);
        }catch(UnknownHostException e){//用户地址出出出出出事了
            System.out.println("有问题,还他妈搁那里凉快呢,我问你话呢,客户端");
        } catch (IOException e) {//IO异常:连接失效,客户端主动退出
            System.out.println("客户端都他娘跑球了:断开连接");
        }
    }
    public void start(){
        PrintWriter byd = null;//置空流--赋值--关流
        try {
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os, "UTF8");
            BufferedWriter bw = new BufferedWriter(osw);
            byd = new PrintWriter(bw, true);
            System.out.println("老弟们,快输入蚊子");
            Scanner input = new Scanner(System.in);
            while (true) {
                String say = input.nextLine();
                byd.println(say);//写出文字
                System.out.println("写完了");
            }
        } catch (UnsupportedEncodingException e) {
            System.out.println("客户局跟服务器端:奶奶的,字符集都他凉的不一致!");
        }catch (IOException e) {
            System.out.println("客户端都他娘断了!");
        } finally {
            //关闭流和socket
            if(byd!=null){
                byd.close();
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("客户端滚蛋了");
                }
            }
        }
    }
    public static void main(String[] args){
        Client client=new Client();
        client.start();
    }
}
